package mybatis.studymybatis;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;
import java.util.Map;

/**
 * @author zhangzg
 * @date 2023/4/4
 */
public class ZhiGanImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry,
        BeanNameGenerator importBeanNameGenerator) {

        //获取扫描路径
        Map<String, Object> annotationAttributes =
            importingClassMetadata.getAnnotationAttributes(ZhiGanMapperScanner.class.getName());
        String path = (String)annotationAttributes.get("value");
        System.out.println("扫描路径:======="+path);
        //获取扫描器
        ZhiGanBeanDefinitionScanner scanner = new ZhiGanBeanDefinitionScanner(registry);
        scanner.addIncludeFilter(new TypeFilter() {
            @Override
            public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
                throws IOException {
                return true;
            }
        });

        scanner.doScan(path);

    }
}
